//
//  SDCControlView.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2018/12/19.
//  Copyright © 2018年 郑逸凯. All rights reserved.
//

#import "SDCControlView.h"

@interface SDCControlView ()

@property (nonatomic,strong)NSMutableArray *btnArray;
@property (nonatomic,strong)UIImageView *backImageView;
@property (nonatomic,strong)UIImageView *centerImageView;
@end


@implementation SDCControlView

#pragma mark - Life Cycle

- (instancetype)init {
    if (self = [super init]) {
        [self addSubview:self.backImageView];
        [self addSubview:self.centerImageView];
        [self setupButtons];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat btnWidth = 40;
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    UIButton *upBtn = self.btnArray[0];
    UIButton *leftBtn = self.btnArray[1];
    UIButton *downBtn = self.btnArray[2];
    UIButton *rightBtn = self.btnArray[3];
    UIButton *centerBtn = self.btnArray[4];
    
    upBtn.frame = CGRectMake((width-btnWidth)/2, 0, btnWidth, btnWidth);
    downBtn.frame = CGRectMake(upBtn.frame.origin.x, height - btnWidth, btnWidth, btnWidth);
    leftBtn.frame = CGRectMake(0, (height-btnWidth)/2, btnWidth, btnWidth);
    rightBtn.frame = CGRectMake(height - btnWidth, leftBtn.frame.origin.y, btnWidth, btnWidth);
    centerBtn.frame = CGRectMake((width - btnWidth)/2, (height - btnWidth)/2, btnWidth, btnWidth);
    CGFloat centerWidth = 61;
    self.backImageView.frame = self.bounds;
    self.centerImageView.frame = CGRectMake((width - centerWidth)/2, (height - centerWidth)/2, centerWidth, centerWidth);
}

#pragma mark - Public Methods
#pragma mark - Responder Actions
- (void)btnClicked:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(SDCControlViewTouched:controlDirection:)]) {
        [self.delegate SDCControlViewTouched:self controlDirection:sender.tag];
    }
}
#pragma mark - Delegates
#pragma mark - Private Methods
- (void)setupButtons {
    NSArray *btnNameArray = @[@"mon_ctrl_up",@"mon_ctrl_left",@"mon_ctrl_down",@"mon_ctrl_right",@"mon_ctrl_shot"];
    self.btnArray = [NSMutableArray array];
    for (NSInteger i = 0; i<btnNameArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:btnNameArray[i]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i + 1;
        [self addSubview:btn];
        [self.btnArray addObject:btn];
    }
   
}

#pragma mark - Setters and Getters
- (UIImageView *)backImageView {
    if (!_backImageView) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mon_big_circle"]];
        imageView.userInteractionEnabled = YES;
        _backImageView = imageView;
    }
    return _backImageView;
}

- (UIImageView *)centerImageView {
    if (!_centerImageView) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mon_small_circle"]];
        imageView.userInteractionEnabled = YES;
        _centerImageView = imageView;
    }
    return _centerImageView;
}



#pragma mark - Helper

@end
